﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using System.Collections.ObjectModel;
using bdnt_pets_system.Tools;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.Failures
{
    public partial class VehicleManagerControl : BaseUserControl
    {
        DataServiceReference.DataServiceClient dataClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;
        public VehicleManagerControl()
        {
            InitializeComponent();
            dataClient = new DataServiceReference.DataServiceClient();
            InitData();
            InitEvent();
            QueryData();
        }
        Tbl_Vehicle CuTbl_Vehicle
        {
            get { return this.DataContext as Tbl_Vehicle; }
            set { this.DataContext = value; }
        }
        //默认查询条件
        Tbl_Vehicle selectVehicle = new Tbl_Vehicle()
        {
            IsValidate = false,
            ProducitonNo = MyStrings.qsrsclsh,
            VINNo = MyStrings.qsrsccjh,
            VPhase = MyStrings.qsrsc_sj,
            VType = MyStrings.qsrclxh,
            VColor = MyStrings.qsrys
        };

        void InitData()
        {
            tbProducitonNo.Foreground = tbVINNo.Foreground =
                tbVPhase.Foreground = tbVType.Foreground = tbVColor.Foreground = new SolidColorBrush(Colors.Gray);

            tbProducitonNo.SetBinding(TextBox.TextProperty, new Binding("ProducitonNo") { Mode = BindingMode.TwoWay });
            tbProducitonNo.Tag = selectVehicle.ProducitonNo;
            tbVINNo.SetBinding(TextBox.TextProperty, new Binding("VINNo") { Mode = BindingMode.TwoWay });
            tbVINNo.Tag = selectVehicle.VINNo;
            tbVPhase.SetBinding(TextBox.TextProperty, new Binding("VPhase") { Mode = BindingMode.TwoWay });
            tbVPhase.Tag = selectVehicle.VPhase;
            tbVType.SetBinding(TextBox.TextProperty, new Binding("VType") { Mode = BindingMode.TwoWay });
            tbVType.Tag = selectVehicle.VType;
            tbVColor.SetBinding(TextBox.TextProperty, new Binding("VColor") { Mode = BindingMode.TwoWay });
            tbVColor.Tag = selectVehicle.VColor;

            CuTbl_Vehicle = new Tbl_Vehicle()
            {
                IsValidate = false,
                ProducitonNo = selectVehicle.ProducitonNo,
                VINNo = selectVehicle.VINNo,
                VPhase = selectVehicle.VPhase,
                VType = selectVehicle.VType,
                VColor = selectVehicle.VColor
            };
        }

        void InitEvent()
        {
            btnNew.Click += new RoutedEventHandler(btnNew_Click);
            btnSelect.Click += new RoutedEventHandler(btnSelect_Click);
            dataClient.GetVehicleListByConditionCompleted += new EventHandler<DataServiceReference.GetVehicleListByConditionCompletedEventArgs>(dataClient_GetVehicleListByConditionCompleted);
            dataClient.GetVehicleListByConditionCountCompleted += new EventHandler<DataServiceReference.GetVehicleListByConditionCountCompletedEventArgs>(dataClient_GetVehicleListByConditionCountCompleted);
            dataClient.DeleteCuVehicleCompleted += new EventHandler<DataServiceReference.DeleteCuVehicleCompletedEventArgs>(dataClient_DeleteCuVehicleCompleted);
            vehiclePageC.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);

        }
        #region 查询与分页

        //数据库查询条件对象
        Tbl_Vehicle selecV = new Tbl_Vehicle();

        void QueryData()
        {
            selecV = new Tbl_Vehicle()
            {
                IsValidate = false,
                ProducitonNo = CuTbl_Vehicle.ProducitonNo == selectVehicle.ProducitonNo ? "" : CuTbl_Vehicle.ProducitonNo,
                VINNo = CuTbl_Vehicle.VINNo == selectVehicle.VINNo ? "" : CuTbl_Vehicle.VINNo,
                VPhase = CuTbl_Vehicle.VPhase == selectVehicle.VPhase ? "" : CuTbl_Vehicle.VPhase,
                VType = CuTbl_Vehicle.VType == selectVehicle.VType ? "" : CuTbl_Vehicle.VType,
                VColor = CuTbl_Vehicle.VColor == selectVehicle.VColor ? "" : CuTbl_Vehicle.VColor
            };
            dataClient.GetVehicleListByConditionCountAsync(selecV);
        }

        void dataClient_GetVehicleListByConditionCountCompleted(object sender, DataServiceReference.GetVehicleListByConditionCountCompletedEventArgs e)
        {
            vehiclePageC.LoadPages(e.Result, pageSize);
        }

        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = vehiclePageC.PageIndex + 1;
            dataClient.GetVehicleListByConditionAsync(selecV, pageSize, currentPage);
        }

        void dataClient_GetVehicleListByConditionCompleted(object sender, DataServiceReference.GetVehicleListByConditionCompletedEventArgs e)
        {
            gdList.ItemsSource = e.Result;
        }

        #endregion

        void dataClient_DeleteCuVehicleCompleted(object sender, DataServiceReference.DeleteCuVehicleCompletedEventArgs e)
        {
            if (e.Result)
                QueryData();
        }

        void btnSelect_Click(object sender, RoutedEventArgs e)
        {
            QueryData();
        }

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            var cupage = new NewVehicleWindow(new Tbl_Vehicle());
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Vehicle;
            var cupage = new NewVehicleWindow(cuObj);
            cupage.Show();
            cupage.Closed += new EventHandler(cupage_Closed);
        }

        void cupage_Closed(object sender, EventArgs e)
        {
            if ((sender as ChildWindow).DialogResult == true)
                QueryData();
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm,MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuObj = (sender as HyperlinkButton).Tag as Tbl_Vehicle;
            dataClient.DeleteCuVehicleAsync(cuObj.Id);
        }
    }

}
